package com.leetcode;

/**
 * @Classname Leetcode309
 * @Description TODO
 * @Date 2021-08-01 20:58
 * @Created by darrren
 */
public class Leetcode309 {
    //0.不持股且非冷冻期,定义其最大收益dp[i][0];
    //1.持股,定义其最大收益dp[i][1]；
    //2.不持股且冷冻期,定义其最大收益dp[i][2];
    public int maxProfit(int[] prices) {
        int n = prices.length;
        if (n <= 1) return 0;

        int[][] dp = new int[n][3];
        dp[0][0] = 0;
        dp[0][1] = -1 * prices[0];
        dp[0][2] = 0;

        for (int i = 1; i < n; i++) {//从[1]...[n-1]
            //不持股，冷冻期或者非冷冻期,取最大的收益
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][2]);
            //持股
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
            //不持股，冷冻期
            dp[i][2] = dp[i - 1][1] + prices[i];

        }

        //非持股状态
        return Math.max(dp[n - 1][0], dp[n - 1][2]);
    }

    public static void main(String[] args) {
        Leetcode309 leetcode = new Leetcode309();
//        int[] prices = {1,2,4};
        int[] prices = {1, 2, 3, 0, 2};
        System.out.println(leetcode.maxProfit(prices));
    }

}
